package com.usian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.usian.config.ResponseResult;
import com.usian.pojo.Supply;
import com.usian.service.SupplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/supply")
public class SupplyController {
    @Autowired
    private SupplyService supplyService;
    @RequestMapping("/findAll")
    public List<Supply> findAll(@RequestBody Supply supply){
        QueryWrapper<Supply> qw = new QueryWrapper<>();
        if (StringUtils.isNotBlank(supply.getName())){
            qw.like("name",supply.getName());
        }
        return supplyService.list(qw);
    }
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody Supply supply){
        try {
            supply.setTime(new Date());
            supply.setGid("DE-TYU-001");
            supplyService.save(supply);
            return ResponseResult.errorResult(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(500,"添加失败");
        }
    }
    @PostMapping("/update")
    public ResponseResult update(@RequestBody Supply supply){
        try {
            supply.setTime(new Date());
            supply.setGid("DE-TYU-001");
            supplyService.updateById(supply);
            return ResponseResult.errorResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(500,"修改失败");
        }
    }
    @PostMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Integer id){
        try {
            supplyService.removeById(id);
            return ResponseResult.errorResult(200,"删除成功");

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(500,"删除失败");
        }
    }
    @GetMapping("/findAllByid")
    public Supply findAllByid(@RequestParam("id") Integer id){
        return supplyService.getById(id);
    }
}
